package com.DuoXianCheng.D12;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class C01 extends Thread {
    // 这个类所有的对象都共享 ticket
    static int ticket = 0;
    // 锁对象，一定要是唯一的
    static Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (ticket < 100) {
                    Thread.sleep(10);
                    ticket++;
                    System.out.println(getName() + "正在买第" + ticket + "张票!");
                } else {
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
